# -*- coding: utf-8 -*-
"""
Created on Tue Jul  6 01:30:33 2021

@author: leonardo.w
"""

# -*- coding: utf-8 -*-
"""
Created on Sun Jul  4 02:39:59 2021

@author: leonardo.w
"""
import tushare as ts
import pandas as pd
import time
import datetime

from pymongo import MongoClient
import json

mytoken = '7871fba9382983fe6ad63fb5cf584265b28d1dddd095caf3429feba9'
ts.set_token(mytoken)
save_path = 'Futures'
pro = ts.pro_api()

def saveData(data):

    client = MongoClient('47.100.19.231',27018)
    db = client.admin
    db.authenticate("quant","Qweasd123")
    db = client.quant

    account = db.futData

    try:
        account.insert_many(json.loads(data.T.to_json()).values())
        print('插入成功')
    except Exception as e:
        print('插入失败！！！！！！',e)
        pass

def get_futures():
        
    # 读取期货主力数据
    tmp_list = []
    for i in ['CFFEX', 'DCE', 'CZCE', 'SHFE', 'INE']:
        df = pro.fut_basic(exchange=i, fut_type='2', fields='ts_code,symbol,name')
        tmp_list.append(df)
    time.sleep(3)
    qh_info = pd.concat(tmp_list)
    
    # 挑选关键字进行删除筛选
    for i in ['连续', '季', '月']:
        qh_info1 = qh_info[qh_info['name'].str.contains(i)]
        qh_info = pd.concat([qh_info, qh_info1, qh_info1]).drop_duplicates(keep=False)
    # qh_info.to_excel(os.path.join('company_info.xlsx'), index=False, encoding='utf-8')
    
    for i in list(qh_info['ts_code']):
    # for i in ['NI.SHF']:
        hy_df = pro.fut_mapping(ts_code=i)
    # hy_df = hy_df.sort_values('trade_date', ascending=True).reset_index(drop=True)
    
        i_code = hy_df['mapping_ts_code'].unique()
        # print(i_code)
        temp_list = []
        for ii in i_code:
            temp_data = hy_df[hy_df['mapping_ts_code'].isin([ii])]
            df = ts.pro_bar(ts_code=str(ii), asset='FT', freq='5min', 
                              start_date= datetime.datetime.strptime(temp_data['trade_date'].min(), '%Y%m%d').strftime('%Y-%m-%d')+' 09:00:00',  
                              end_date = datetime.datetime.strptime(temp_data['trade_date'].max(), '%Y%m%d').strftime('%Y-%m-%d')+' 17:00:00')
            print(ii+'已读取')
            temp_list.append(df)
            time.sleep(15)
        
        tmp_df = pd.concat(temp_list)
        tmp_df['tz_code'] = str(i)
        tmp_df = tmp_df.sort_values('trade_time', ascending=True).reset_index(drop=True)
        # tmp_df = tmp_df.drop(tmp_df[tmp_df.trade_time < '2016-01-01 00:00:00'].index)
        tmp_df.fillna(method='ffill')
        tmp_df = tmp_df.drop_duplicates(subset = 'trade_time', keep = 'first')
        try:
            tmp_df = tmp_df.sort_values('trade_time', ascending=True)
            
            print(tmp_df)
            saveData(tmp_df)
            # # 存为pickle文件方便读取
            # with open(save_path+'\OldData\Data_'+ i + '.pkl', 'wb') as file:
            #     pickle.dump(tmp_df, file)
            # print(i+'已完成')
        except:
            print(i+'数据异常')

if __name__ == '__main__':
    # if not os.path.exists('Futures'):
    #     os.mkdir('Futures')
    #     os.mkdir('Futures/OldData')
    #主程序
    get_futures()
    

